package com.gcstorage.tvdev.util;

import android.text.TextUtils;

import com.gcstorage.tvdev.bean.UserInfo;
import com.gcstorage.tvdev.constants.AppConstant;
import com.gcstorage.tvdev.constants.KeyCode;
import com.gcstorage.tvdev.net.ApiAddress;
import com.gcstorage.tvdev.net.HttpClient;
import com.gcstorage.tvdev.net.RxObserver;
import com.google.gson.Gson;
import com.google.gson.JsonElement;

import java.util.Collections;


/**
 * 用户数据管理类
 * created by Sanpark on 2020-10-27 09:28
 */
public class UserHelper {
    private static UserHelper instance;

    private UserHelper() {
    }

    public static UserHelper instance() {
        if (instance == null) {
            synchronized (UserHelper.class) {
                if (instance == null) {
                    instance = new UserHelper();
                }
            }
        }
        return instance;
    }


    public UserInfo getUserInfo() {
        UserInfo userInfo = new UserInfo();
        String userInfoData = SpUtils.getString(SpUtils.TAG_USER, KeyCode.USER_INFO, "");
        if (!TextUtils.isEmpty(userInfoData)) {
            userInfo = new Gson().fromJson(userInfoData, UserInfo.class);
        }
        return userInfo;
    }

    public String getUserId() {
        return getUserInfo().getUserId();
    }

    private void saveToSpUtils(UserInfo entity) {
        SpUtils.putString(SpUtils.TAG_USER, KeyCode.USER_INFO, new Gson().toJson(entity));
    }

    public void setLoginStatus(boolean isLogin) {
        SpUtils.putBoolean(SpUtils.TAG_USER, KeyCode.USER_LOGIN_STATUS, isLogin);
    }

    public boolean getLoginStatus() {
        return SpUtils.getBoolean(SpUtils.TAG_USER, KeyCode.USER_LOGIN_STATUS, false);
    }

    public void updateUserInfo() {
        HttpClient.get().get(AppConstant.getApiAddress() + ApiAddress.USER_INFO,
                Collections.singletonMap("userId", SpUtils.getString(SpUtils.TAG_USER, KeyCode.USER_ID, "")), new RxObserver() {
                    @Override
                    public void onSuccess(JsonElement t) {
                        UserInfo userInfo = GsonUtils.covertObject(t, UserInfo.class).getData();
                        UserHelper.instance().loginInUser(userInfo);
                    }
                });
    }


    //退出账户
    public void logoutUser() {
        setLoginStatus(false);
        SpUtils.clear(SpUtils.TAG_USER);
        JumpHelper.gotoLoginActivity();
    }

    //登陆成功
    public void loginInUser(UserInfo userInfo) {
        setLoginStatus(true);
        userInfo.setLoginToken(SpUtils.getString(SpUtils.TAG_USER, KeyCode.USER_ACCESS_TOKEN, ""));
        saveToSpUtils(userInfo);
//        AppDatabase.getInstance(BaseApplication.getInstance()).userInfoDao().insert(userInfo);
    }

//    public List<UserInfo> convertMember(List<PersonInfo> memberInfos) {
//        List<UserInfo> userInfoList = new ArrayList<>();
//        for (int i = 0; i < memberInfos.size(); i++) {
//            PersonInfo info = memberInfos.get(i);
//            UserInfo item = new UserInfo();
//            item.setRealName(info.getRealName());
//            item.setAccount(info.getAccount());
//            item.setUserId(info.getUserId());
//            item.setPhone(info.getPhone());
//            item.setAvatar(info.getAvatar());
//            item.setPoliceNo(info.getPoliceNo());
//            userInfoList.add(item);
//        }
//        return userInfoList;
//    }

}
